package com.ashin.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 用于演示各种参数绑定方式的demo示例
 *
 * @Author: Ashin
 * @Date: 2021/1/15
 * @Description: com.ashin.controller
 * @Version: 1.0.0
 */
@Controller
@RequestMapping("/input")
public class ParamMappingDemoController {


    @RequestMapping("/path_variable/{msg}")
    public String demoPathVariable(@PathVariable String msg) {
        System.out.println(msg);

        return "views/" + msg;
    }

    @RequestMapping("/request_param")
    public String demoReqeuestParam(@RequestParam(name = "name") String name, @RequestParam(name = "age", required = false, defaultValue = "0") int age) {
        System.out.println(name);
        System.out.println(age);

        return "views/success";
    }

    /**
     * 请求头名称大小写不区分
     *
     * @param encoding
     * @return
     */
    @RequestMapping("/request_header")
    public String demoReqeuestHeader(@RequestHeader("Accept-encoding") String encoding, @RequestHeader("cookie") String cookie) {
        System.out.println(encoding);
        System.out.println(cookie);

        return "views/success";
    }

    /**
     * 请求头名称大小写不区分
     *
     * @param id
     * @return
     */
    @RequestMapping("/cookie_value")
    public String demoCookieValue(@CookieValue("JSESSIONID") String id) {
        System.out.println(id);

        return "views/success";
    }

    /**
     * body是一个json对象Content-Type: application/json
     *
     * @param user
     * @return
     */
    @RequestMapping("/request_body")
    public String demoRequestBody(@RequestBody User user) {
        System.out.println("request body演示");
        System.out.println(user);

        return "views/success";
    }

    @RequestMapping("/plain_form")
    public String demoPojo(String name, Integer age, Boolean sex) {
        System.out.println("request pojo演示");
        System.out.println(name);
        System.out.println(age);
        System.out.println(sex);

        return "views/success";
    }

    /**
     * 普通对象，请求Content-Type: application/x-www-form-urlencoded
     * 即表单形式
     *
     * @param user
     * @return
     */
    @RequestMapping(value = "/pojo",method = RequestMethod.POST)
    public String demoPojo(User user) {
        System.out.println("request pojo演示");
        System.out.println(user);

        return "views/success";
    }

    /**
     *
     * @param user
     * @return
     */
    @RequestMapping("/map")
    public String demoMap(Map user) {
        System.out.println("request pojo演示");
        System.out.println(user);

        return "views/success";
    }

    /**
     * HttpServletRequest
     * HttpServletResponse
     * HttpSession
     * java.security.Principal
     * Locale
     * InputStream
     * OutputStream
     * Reader
     * Writer
     *
     * @return
     */
    @RequestMapping("/servlet_api_object")
    public String demoServletApiObject(User user, HttpServletRequest req) {
        System.out.println("servlet_api_object演示");
        System.out.println(user);
        System.out.println(req);

        return "views/success";
    }


    private static class User {
        private String name;
        private String age;
        private Address address;
        private Map<String, Object> paramsMap;


        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getAge() {
            return age;
        }

        public void setAge(String age) {
            this.age = age;
        }

        public Address getAddress() {
            return address;
        }

        public void setAddress(Address address) {
            this.address = address;
        }

        public Map<String, Object> getParamsMap() {
            return paramsMap;
        }

        public void setParamsMap(Map<String, Object> paramsMap) {
            this.paramsMap = paramsMap;
        }

        @Override
        public String toString() {
            return "User{" +
                    "name='" + name + '\'' +
                    ", age='" + age + '\'' +
                    ", address=" + address +
                    '}';
        }
    }

    public static class Address {
        private String province;
        private String city;

        public String getProvince() {
            return province;
        }

        public void setProvince(String province) {
            this.province = province;
        }

        public String getCity() {
            return city;
        }

        public void setCity(String city) {
            this.city = city;
        }

        @Override
        public String toString() {
            return "Address{" +
                    "province='" + province + '\'' +
                    ", city='" + city + '\'' +
                    '}';
        }
    }


}
